#include<avr/io.h>
#include "../adc/spi.h"
#include "../prog_res/prog_res.h"

void Init_prog_res(void)
{
CS1_RES_CONF;
CS1_RES_DISABLE;
CS2_RES_CONF;
CS2_RES_DISABLE;
CS3_RES_CONF;
CS3_RES_DISABLE;
CS4_RES_CONF;
CS4_RES_DISABLE;
}



void put_res(unsigned char num_res,unsigned char num_channel,unsigned char res)
{
 
 unsigned short i=0;

 
 SPI_PORT_INIT;

 SCK_0;

 switch(num_res)
 {
	 case 0:
	 	CS1_RES_ENABLE;
		break;
	 case 3:
	 	CS4_RES_ENABLE;
		break;
 }

 if (num_channel) MOSI_1; else MOSI_0;
 asm("nop");
 SCK_1;
 asm("nop");
 SCK_0;
  for(i=0;i<8;i++)
  {
   if (res&(1<<(7-i))) MOSI_1; else MOSI_0;
   asm("nop");
   SCK_1;
   asm("nop");
   SCK_0;
  }
 
  switch(num_res)
  {
	 case 0:
 		CS1_RES_DISABLE;
		break;
	 case 3:
 		CS4_RES_DISABLE;
		break;
  };

};








void put_res_bug(unsigned char num_res,unsigned char num_channel,unsigned char res)
{
 
 unsigned short i=0;

 
 SPI_PORT_INIT_BUG;

 SCK_0;

 switch(num_res)
 {
	 case 1:
		CS2_RES_ENABLE;
		break;
	 case 2:
	 	CS3_RES_ENABLE;
		break;
 }

 if (num_channel) MOSI_1_BUG; else MOSI_0_BUG;
 asm("nop");
 SCK_1;
 asm("nop");
 SCK_0;
  for(i=0;i<8;i++)
  {
   if (res&(1<<(7-i))) MOSI_1_BUG; else MOSI_0_BUG;
   asm("nop");
   SCK_1;
   asm("nop");
   SCK_0;
  }
 
  switch(num_res)
  {
	 case 1:
 		CS2_RES_DISABLE;
		break;
	 case 2:
 		CS3_RES_DISABLE;
		break;
  };
  SPI_OFF_BUG;
};
